﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EFPlusDemo.Core.Entities;
using EFPlusDemo.Data.Repositories;
using EntityFrameworkPlus.Data.Extenstions;
using EntityFrameworkPlus.Framework;
using NUnit.Framework;

namespace EFPlusDemo.Tests
{
    [TestFixture]
    public class RepositoryTest
    {
        private readonly ProductInfoRepository _productInfoRepository = new ProductInfoRepository();
        private readonly UserInfoRepository _userInfoRepository = new UserInfoRepository();

        [SetUp]
        public void Init()
        {
            // 初始化数据库
            DbContextManager.InitStorage(new SimpleDbContextStorage());
            // 由于这里是测试项目，因此其dll路径有所不同
            DbContextManager.InitDbContext("UserDB", "../Debug/EFPlusDemo.Data.dll", "EFPlusDemo.Data.Mapping.UserDB");
            DbContextManager.InitDbContext("ProductDB", "../Debug/EFPlusDemo.Data.dll", "EFPlusDemo.Data.Mapping.ProductDB");
        }

        /// <summary>
        /// UserInfo新增测试
        /// </summary>
        [Test]
        public void 新增测试_UserInfo()
        {
            var user = new UserInfo
                           {
                               Password = "password1123",
                               RegisterTime = DateTime.Now,
                               SafeMoney = 100.00M,
                               UserName = "testName"
                           };
            _userInfoRepository.Add(user);
            _userInfoRepository.UnitOfWork.SaveChanges();
        }

        /// <summary>
        /// ProductInfo新增测试
        /// </summary>
        [Test]
        public void 新增测试_ProductInfo()
        {
            var product = new ProductInfo
                              {
                                  CreateTime = DateTime.Now,
                                  Price = 1000.00M,
                                  ProductName = "TestProduct"
                              };
            _productInfoRepository.Add(product);
            _productInfoRepository.UnitOfWork.SaveChanges();
        }

        [Test]
        public void 工作单元的新增测试()
        {
            // 批量生成10条数据
            var userInfoList = new List<UserInfo>();
            var productInfoList = new List<ProductInfo>();
            for (var j = 0; j < 10; j++)
            {
                var user = new UserInfo
                               {
                                   Password = "password" + j,
                                   RegisterTime = DateTime.Now,
                                   SafeMoney = 100.00M,
                                   UserName = "UserName" + j
                               };
                userInfoList.Add(user);

                var product = new ProductInfo
                                  {
                                      CreateTime = DateTime.Now,
                                      Price = 100.00M,
                                      ProductName = "ProductName" + j
                                  };
                productInfoList.Add(product);
            }

            // 新增
            foreach (var userInfo in userInfoList)
                _userInfoRepository.Add(userInfo);
            foreach (var productInfo in productInfoList)
                _productInfoRepository.Add(productInfo);

            // 一次提交 
            _userInfoRepository.UnitOfWork.SaveChanges();
            _productInfoRepository.UnitOfWork.SaveChanges();

            // 具体SQL可通过SqlServer Profiler查看
        }

        [Test]
        public void 多条件查询测试()
        {
            var predicate = QueryBuild<UserInfo>.True();
            // 如果某个条件（比如UserName不为空)
            // if(!string.IsNullOrEmpty(userName))
            predicate = predicate.And(c => c.UserName.Contains("userName"));
            // 如果某个条件（比如SafeMoney)
            predicate = predicate.And(c => c.SafeMoney > 10 && c.SafeMoney < 1000);
            // .....

            var list = _userInfoRepository.GetList(predicate);

            Console.WriteLine(predicate.ToString());

            foreach (var userInfo in list)
            {
                Console.WriteLine(string.Format("{0},{1},{2},{3},{4}", userInfo.UserId, userInfo.UserName,
                                                userInfo.Password, userInfo.SafeMoney, userInfo.RegisterTime));
            }
        }

        [Test]
        public void 排序分页测试()
        {
            var predicate = QueryBuild<ProductInfo>.True();
            predicate = predicate.And(c => c.Price > 90 && c.Price <= 100);
            predicate = predicate.And(c => c.ProductName.Contains("ProductName"));
            const string sortKey = "CreateTime";
            const bool @ascending = true;
            const int pageIndex = 1;
            const int pageSize = 5;

            var pagedList = _productInfoRepository.GetPagedList(predicate, sortKey, ascending, pageIndex, pageSize);

            Console.WriteLine("总记录条数:{0}", pagedList.TotalItemCount);
            Console.WriteLine("总页数:{0}", pagedList.TotalPageCount);
            Console.WriteLine("当前页:{0}", pagedList.CurrentPageIndex);
            Console.WriteLine("页大小:{0}", pagedList.PageSize);
            Console.WriteLine("页列表:");
            foreach (var productInfo in pagedList)
            {
                Console.WriteLine("{0},{1},{2},{3}", productInfo.ProductId, productInfo.ProductName, productInfo.Price,
                                  productInfo.CreateTime);
            }
        }

    }
}
